package com.liyunc.demo.custom.livechat.client;

import com.liyunc.demo.custom.livechat.client.utils.ImageUtil;
import com.liyunc.demo.custom.livechat.manager.ClientFactory;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChatWindow extends JFrame {

    private JFrame chatWindow;
    public JTextArea contentArea;
    private JTextField txt_message;
    private JTextField userName;
    private JButton btn_update;
    private JButton btn_send;
    private JPanel northPanel;
    private JPanel southPanel;
    private JScrollPane rightPanel;
    private JScrollPane leftPanel;
    private JSplitPane centerSplit;
    private JList userList;
    private DefaultListModel listModel;

    public ChatWindow() {
        chatWindow = new JFrame("LiveChat");
        chatWindow.setIconImage(ImageUtil.getMainImage());
        chatWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        contentArea = new JTextArea();
        contentArea.setEditable(false);
        contentArea.setForeground(Color.blue);
        txt_message = new JTextField();
        userName = new JTextField();
        btn_update = new JButton("修改信息");
        btn_send = new JButton("发送");
        listModel = new DefaultListModel();
        userList = new JList(listModel);

        southPanel = new JPanel(new BorderLayout());
        southPanel.setBorder(new TitledBorder("写消息"));
        southPanel.add(txt_message, "Center");
        southPanel.add(btn_send, "East");
        leftPanel = new JScrollPane(userList);
        leftPanel.setBorder(new TitledBorder("在线用户"));

        rightPanel = new JScrollPane(contentArea);
        rightPanel.setBorder(new TitledBorder("消息显示区"));

        centerSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,
            rightPanel);
        centerSplit.setDividerLocation(100);

        northPanel = new JPanel();
        northPanel.setLayout(new GridLayout(1, 6));
        northPanel.add(new Label());
        northPanel.add(new Label());
        northPanel.add(new Label());
        northPanel.add(new JLabel("姓名"));
        northPanel.add(userName);
        northPanel.add(btn_update);
        northPanel.setBorder(new TitledBorder("配置信息"));

        chatWindow.setLayout(new BorderLayout());
        chatWindow.add(northPanel, "North");
        chatWindow.add(centerSplit, "Center");
        chatWindow.add(southPanel, "South");
        chatWindow.setSize(600, 400);
        chatWindow.setLocationRelativeTo(null);
        chatWindow.setVisible(true);

        //文本框按回车事件
        txt_message.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        btn_send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });
    }

    /**
     * 发送消息
     */
    private void sendMessage() {
        String message = txt_message.getText().trim();
        if ("".equals(message)) {
            JOptionPane.showMessageDialog(chatWindow, "消息不能为空！", "错误",
                JOptionPane.ERROR_MESSAGE);
        } else {
            new SendMes(ClientFactory.getSocketClient().socket, message).start();
            new ReadMes(ClientFactory.getSocketClient().socket).start();
            txt_message.setText(null);
        }
    }
}
